package service

import (
	"github.com/gin-gonic/gin"
	"kubequant/apps/cta"
	"kubequant/trader"
	"net/http"
	"strings"
)

var mainEngine = trader.NewMainEngine()

// AddApp 添加app
func AddApp(c *gin.Context) {
	name := c.Param("name")
	name = strings.Trim(name, "/")
	_, err := mainEngine.AddApp(name)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
	}
	c.JSON(http.StatusOK, gin.H{"msg": "添加app成功:" + name})
}

// InitEngine 初始化引擎
func InitEngine(c *gin.Context) {
	name := c.Param("name")
	name = strings.Trim(name, "/")
	engine, err := mainEngine.GetEngine(name)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		return
	}
	if name == "cta" {
		ctaEngine := engine.(*cta.Engine)
		err = ctaEngine.InitEngine()
	}
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "引擎初始化成功:" + name})
	}
}
